import requests
from bs4 import BeautifulSoup
import lxml
import pymysql


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    resp = requests.get('http://www.baidu.com/')
    print(resp.text)

    bsobj = BeautifulSoup(resp.content,'lxml')
    a_list = bsobj.find_all('a')
    for a in a_list:
        print(a.get('href'))
        # 打开数据库连接
        db = pymysql.connect(host="localhost", port=3306, user="root", password="123456", database="test")

        # 使用 cursor() 方法创建一个游标对象 cursor
        cursor = db.cursor()
        sql = "INSERT INTO user(user_name) VALUES ('"+a.get('href')+"')"
        try:
            cursor.execute(sql)
            db.commit()
        except:
            db.rollback()

        db.close()



    # db = pymysql.connect("localhost", "testuser", "test123", "test")
    #
    # # 使用cursor()方法获取操作游标
    # cursor = db.cursor()
    #
    # # SQL 插入语句
    # sql = """INSERT INTO EMPLOYEE(FIRST_NAME,
    #          LAST_NAME, AGE, SEX, INCOME)
    #          VALUES ('Mac', 'Mohan', 20, 'M', 2000)"""
    # try:
    #     # 执行sql语句
    #     cursor.execute(sql)
    #     # 提交到数据库执行
    #     db.commit()
    # except:
    #     # 如果发生错误则回滚
    #     db.rollback()
    #
    # # 关闭数据库连接
    # db.close()
    #
    # # 打开数据库连接
    # db = pymysql.connect("localhost", "testuser", "test123", "TESTDB")
    #
    # # 使用cursor()方法获取操作游标
    # cursor = db.cursor()
    #
    # # SQL 查询语句
    # sql = "SELECT * FROM EMPLOYEE \
    #        WHERE INCOME > %s" % (1000)
    # try:
    #     # 执行SQL语句
    #     cursor.execute(sql)
    #     # 获取所有记录列表
    #     results = cursor.fetchall()
    #     for row in results:
    #         fname = row[0]
    #         lname = row[1]
    #         age = row[2]
    #         sex = row[3]
    #         income = row[4]
    #         # 打印结果
    #         print("fname=%s,lname=%s,age=%s,sex=%s,income=%s" % \
    #               (fname, lname, age, sex, income))
    # except:
    #     print("Error: unable to fetch data")
    #
    # # 关闭数据库连接
    # db.close()
    #
    # # 打开数据库连接
    # db = pymysql.connect("localhost", "testuser", "test123", "TESTDB")
    #
    # # 使用cursor()方法获取操作游标
    # cursor = db.cursor()
    #
    # # SQL 更新语句
    # sql = "UPDATE EMPLOYEE SET AGE = AGE + 1 WHERE SEX = '%c'" % ('M')
    # try:
    #     # 执行SQL语句
    #     cursor.execute(sql)
    #     # 提交到数据库执行
    #     db.commit()
    # except:
    #     # 发生错误时回滚
    #     db.rollback()
    #
    # # 关闭数据库连接
    # db.close()